<?php

namespace console\controllers;

use services\infusion\TreatmentService;
use services\patient\ConsumptionDetailService;
use services\patient\UserUpdateService;
use Yii;
use yii\console\Controller;

/**
 * 患者
 *
 * Class PatientController
 * @package console\controllers
 */
class PatientController extends Controller
{
    /**
     * 更新患者未到诊天数
     *
     * 0 0 * * * php /home/www/hcrm/backend/yii patient/update-missed-days >/dev/null 2>&1
     */
    public function actionUpdateMissedDays()
    {
        set_time_limit(0);

        try {
            $startTime = time();

            $res = Yii::$app->services->patientUser->updateMissedDays();

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     *  更新患者治疗,诊室id
     */
    public function actionUpdateClinicId()
    {
        set_time_limit(0);

        $res = Yii::$app->services->clinicTreatmentManage->updateClinicId();

        echo $res;

    }

    /**
     * 一次性脚本
     * 更新患者信息扩展表上次操作时间【输液大厅对患者的药品的最后一次操作时间】
     * yii patient/update-all-operate-time
     */
    public function actionUpdateAllOperateTime()
    {
        $startTime = date('Y-m-d H:i:s');
        $service = new TreatmentService();
        $service->updateAllOperateTime();
        $endTime = date('Y-m-d H:i:s');
        echo "success".PHP_EOL;
        echo "开始时间: {$startTime}".PHP_EOL;
        echo "结束时间: {$endTime}".PHP_EOL;
    }

    /**
     * 修复消费记录【删除重复记录】
     * php yii patient/repair-consumption-detail
     */
    public function actionRepairConsumptionDetail(int $projectId)
    {
        $startTime = date('Y-m-d H:i:s');

        $service = new ConsumptionDetailService();
        $service->repairConsumptionDetail($projectId);

        $endTime = date('Y-m-d H:i:s');
        echo "success".PHP_EOL;
        echo "开始时间: {$startTime}".PHP_EOL;
        echo "结束时间: {$endTime}".PHP_EOL;
    }

    /**
     * 南通项目下的所有患者数据，按照诊室来进行重新分配
     * 数据按时间顺序，每个诊室的数据，AB轮流分
     * php yii patient/update-member-batch
     */
    public function actionUpdateMemberBatch()
    {
        $startTime = date('Y-m-d H:i:s');

        $service = new UserUpdateService();
        $service->updateMemberBatch();

        $endTime = date('Y-m-d H:i:s');
        echo "success".PHP_EOL;
        echo "开始时间: {$startTime}".PHP_EOL;
        echo "结束时间: {$endTime}".PHP_EOL;
    }
}
